package com.sunlg.smarthome.ui.environment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.data.entity.TempAndHum;

import java.util.List;

/**
 * Created by sunlg on 2017/5/10.
 */

public class TempAndHumAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<TempAndHum> mTempAndHumList;

    public TempAndHumAdapter(Context context, List<TempAndHum> tempAndHumList) {
        this.mContext = context;
        this.mTempAndHumList = tempAndHumList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_temp_hum, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;

        final TempAndHum tempAndHum = mTempAndHumList.get(position);
        viewHolder.tvDesc.setText(tempAndHum.getDescription());
        viewHolder.tvTemp.setText(String.valueOf(tempAndHum.getTemperature())+"℃");
        if (tempAndHum.getHumidity() != null) {
            viewHolder.tvHum.setText(String.valueOf(tempAndHum.getHumidity())+"%");
        }
        viewHolder.tvUpdateTime.setText(tempAndHum.getTime());

        viewHolder.cvTempHumItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TempAndHumActivity.class);
                Bundle bundle = new Bundle();


                bundle.putInt("deviceId", tempAndHum.getDeviceId());

                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mTempAndHumList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        CardView cvTempHumItem;
        TextView tvDesc;
        TextView tvTemp;
        TextView tvHum;
        TextView tvUpdateTime;

        public ItemViewHolder(View itemView) {
            super(itemView);
            cvTempHumItem = (CardView) itemView.findViewById(R.id.cv_temp_hum_item);
            tvDesc = (TextView) itemView.findViewById(R.id.tv_desc);
            tvTemp = (TextView) itemView.findViewById(R.id.tv_temp);
            tvHum = (TextView) itemView.findViewById(R.id.tv_hum);
            tvUpdateTime = (TextView) itemView.findViewById(R.id.tv_update_time);
        }
    }
}
